package com.goodfather.textbook.youdao.pad.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.goodfather.textbook.youdao.pad.R;


/**
 * Created by zerup on 17/5/22.
 */

public abstract class DialogBaseV1<V extends View> {
    protected AlertDialog mDialog;
    protected V mCustomView;
    protected Context mContext;

    public boolean isShowing() {
        return isShowing;
    }

    protected boolean isShowing;

    private AlertDialog createDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogTheme);
        AlertDialog dialog = builder.create();
        return dialog;
    }

    protected void create(Context context, V contentView) {
        mCustomView = contentView;
        mContext = context;
        mDialog = createDialog(context);
        mDialog.show();
        mDialog.setContentView(contentView);
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dismiss();
            }
        });
        initView(contentView);
        initData();
    }

    public abstract void initView(View view) ;

    public abstract void initData();

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
            isShowing = false;
        }
    }

    public void setCanceledOnTouchOutside(boolean canceled){
        if (mDialog != null){
            mDialog.setCanceledOnTouchOutside(canceled);
        }
    }

//    public void setWindowPosition(int x, int y) {
//        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
//        params.x = x;
//        params.y = y;
//        params.width = ViewUtil.getScreenWidth(mContext) - params.x;
//        mDialog.getWindow().setAttributes(params);
//    }

    //设置窗口属性

    /**
     *
     * @param cancelable
     * @param gravity
     * @param disableDim 取消背景半透明
     */
    public void setWindowStyle(boolean cancelable, int gravity, boolean disableDim) {
        Window window = mDialog.getWindow();
        window.setGravity(gravity);
        WindowManager.LayoutParams params = window.getAttributes();

        if (Gravity.BOTTOM == gravity) {
            params.windowAnimations = R.style.slide_in_out_from_bottom;
        }
        window.setAttributes(params);
        if (disableDim) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        mDialog.setCancelable(cancelable);
        mDialog.setCanceledOnTouchOutside(false);
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        if (mDialog != null) {
            mDialog.setOnDismissListener(listener);
        } else {
            ToastUtil.show("DialogBase must be shown before setting listener");
        }
    }

}
